package com.cj.mobile.iabp.mvp.presenter;

import static com.cj.mobile.iabp.BuildConfig.IS_DEBUG_DATA;

import com.cj.mobile.iabp.app.config.CommonRetryWithDelay;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.mvp.contract.HomeSearchContract;
import com.cj.mobile.iabp.mvp.model.entity.PatientInfo;
import com.cj.mobile.iabp.mvp.ui.adapter.SearchInfoAdapter;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/08/31 20:27
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HomeSearchPresenter extends BasePresenter<HomeSearchContract.Model, HomeSearchContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    AccountManager mAccountManager;

    // 数据源
    @Inject
    List<PatientInfo> mList;

    // 内容适配器
    @Inject
    SearchInfoAdapter mAdapter;

    @Inject
    public HomeSearchPresenter(HomeSearchContract.Model model, HomeSearchContract.View rootView) {
        super(model, rootView);
    }

    public void initData() {
        // 显示搜索历史记录
        mRootView.addSearchHistory(mAccountManager.getHistorySearchList());
    }

    /**
     * 搜索内容
     *
     * @param key 关键字
     */
    public void searchContent(String key) {

        // 保存搜索记录
        mAccountManager.setHistorySearch(key);

        if (IS_DEBUG_DATA) {
            //清空列表
            mList.clear();

            mList.add(new PatientInfo("", "2025-08-22 08:11:46", "", "", "", "0", "", "90",
                    "2739702", "1", "王越", "结构", "62", "1", "160", "75", "29", "结构性心脏病中心二区",
                    "2", "朝阳院区", "", "", "冠心病", "0", "1", "i3", "20", "", "", "33c9591de1cfba80dfc98facac0c265f", "", ""));
            mList.add(new PatientInfo("", "2025-08-22 08:11:46", "", "", "", "0", "", "84",
                    "52904826", "1", "王美芹", "冠二", "68", "2", "155", "67", "25", "冠心病外科中心二区",
                    "2", "朝阳院区", "右冠起源异常", "", "冠心病", "2", "1", "I5", "16", "", "", "ebeefa95219c9176bd91ec4a8f2681fe", "", ""));
            mList.add(new PatientInfo("", "2025-08-22 08:11:46", "", "", "", "0", "", "72",
                    "60885579", "1", "王润月", "冠二", "70", "1", "162", "75", "25", "冠心病外科中心二区",
                    "2", "朝阳院区", "", "", "冠心病", "2", "1", "i5", "26", "", "", "e4c6f074d06709d80da0da55b9092cd1", "", ""));

            mAdapter.notifyDataSetChanged();
        } else {
            mModel.searchContent(key)
                    .subscribeOn(Schedulers.io())
                    .retryWhen(new CommonRetryWithDelay(2, 2))       // 遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .doOnSubscribe(disposable -> {                                                  // 显示加载效果
                        //显示下拉刷新的进度条
                        mRootView.showLoading();
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> {                                                              // 隐藏加载效果
                        //隐藏下拉刷新的进度条
                        mRootView.hideLoading();
                    })
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .subscribe(new ErrorHandleSubscriber<List<PatientInfo>>(mErrorHandler) {

                        @Override
                        public void onNext(List<PatientInfo> infoResponse) {
                            if (infoResponse != null) {
                                mList.clear();

                                if (CommonUtils.isNotEmpty(infoResponse))
                                    mList.addAll(infoResponse);

                                // 显示“暂无信息”
                                mRootView.isNotInfo(mList.size() == 0);

                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    });

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
    }
}